﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RooHui.OA.Biz;
using RooHui.OA.Web.Model;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.Admin.Web.Controllers
{
    public class PolicyController : Controller
    {
        //
        // GET: /Policy/
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 分页获取政策信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetPolicyListByPage(int page, int pageSize, string key = "")
        {
            int count = 0;
            var policyList = PolicyBiz.GetpolicyListByPage(page, pageSize, key, ref count);
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Rows", policyList);
            dic.Add("Total", count);
            return Content(dic.ToJson());
        }

        /// <summary>
        /// 获取启用状态的特殊策略
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetEnabledpePolicyByStatus()
        {
            var policyList = PolicyBiz.GetSpePolicyByStatus(1);
            List<PolicyShow> policyShowList = new List<PolicyShow>();
            foreach(Policy item in policyList)
            {
                var durationtype = item.DurationType == DurationType.ByDay ? "日" : "月";
                var balancetype = "";
                switch (item.BalanceType)
                {
                    case BalanceType.AverCapitalInterest:
                        balancetype = "每期等额本金利息";
                        break;
                    case BalanceType.EachInterestTerminalCapital:
                        balancetype = "每期结息到期还本";
                        break;
                    case BalanceType.EverageFixed:
                        balancetype = "每期固定收益";
                        break;
                    case BalanceType.TerminalCapitalInterest:
                        balancetype = "到期结本息";
                        break;
                    case BalanceType.TerminalFixed:
                        balancetype = "到期固定收益";
                        break;
                    default:
                        balancetype = "每期等额本金利息";
                        break;
                }
                var settlementtype = item.SettlementType== SettlementType.Fixed ? "固定值" : "按比例";
                PolicyShow temp = new PolicyShow(){
                    Key = item.ID,
                    Value = "[" + item.PolicyName + "]" + "年化收益" + item.Rate + "分" + item.CycleAmount + "期("
                           + item.Duration + durationtype + "/期)" + settlementtype + "以" + balancetype + "结算"
                };
                policyShowList.Add(temp);
            }

            policyShowList.Insert(0, new PolicyShow() { Key = -1, Value = "请选择" });
            return Content(policyShowList.ToJson());
        }

        /// <summary>
        /// 增加/修改政策
        /// </summary>
        /// <param name="editType"></param>
        /// <param name="policyId"></param>
        /// <param name="policyName"></param>
        /// <param name="durationType"></param>
        /// <param name="balanceType"></param>
        /// <param name="rate"></param>
        /// <param name="settlementType"></param>
        /// <param name="duration"></param>
        /// <param name="cycleAmount"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult PolicySave(int editType, int policyId, string policyName, int balanceType, decimal rate, int settlementType, int duration)
        {
            bool bSaveFlag = false;
            if (editType == 1)
            {
                bSaveFlag = PolicyBiz.PolicyAdd(policyName, balanceType, rate, settlementType, duration);
            }
            else if (editType == 2)
            {
                bSaveFlag = PolicyBiz.PolicyEdit(policyId, policyName, balanceType, rate, settlementType, duration);
            }

            return Content(bSaveFlag.ToString());
        }

        /// <summary>
        /// 改变政策状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult PolicyStatusChange(int policyId, int destinationStatus)
        {
            bool bChangeFlag = PolicyBiz.PolicyStatusChange(policyId, destinationStatus);
            return Content(bChangeFlag.ToString());
        }

    }
}
